package com.accompany.service.impl;

import com.accompany.entity.Master;
import com.accompany.dao.MasterMapper;
import com.accompany.service.MasterService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 大神表 服务实现类
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@Slf4j
@Service
public class MasterServiceImpl extends ServiceImpl<MasterMapper, Master> implements MasterService {

    @Autowired
    private MasterMapper masterMapper;

    /**
     * 分页查询大神列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Override
    public Page<Master> getPaginatedMasters(Integer page, Integer size) {
        try {
            log.info("分页查询大神列表，页码：{}, 每页大小：{}", page, size);
            Page<Master> pageObj = new Page<>(page, size);
            Page<Master> masterPage = masterMapper.selectPage(pageObj, null);
            masterPage.getRecords().forEach(item -> log.info(item.toString()));
            return masterPage;
        } catch (Exception e) {
            log.error("分页查询大神列表时发生错误，页码：{}, 每页大小：{}", page, size, e);
            throw e;
        }
    }

    /**
     * 获取所有大神列表
     *
     * @return 大神列表
     */
    @Override
    public List<Master> getAllMasters() {
        try {
            log.info("获取所有大神列表");
            return masterMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有大神列表时发生错误", e);
            throw e;
        }
    }

    /**
     * 获取单个大神信息
     *
     * @param id 大神ID
     * @return 大神对象
     */
    @Override
    public Master getMasterById(Long id) {
        try {
            log.info("获取单个大神信息，大神ID：{}", id);
            return masterMapper.selectById(id);
        } catch (Exception e) {
            log.error("获取单个大神信息时发生错误，大神ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 更新大神信息
     *
     * @param master 大神信息
     */
    @Override
    @Transactional
    public void updateMaster(Master master) {
        try {
            log.info("更新大神信息，大神信息：{}", master.toString());
            masterMapper.updateById(master);
        } catch (Exception e) {
            log.error("更新大神信息时发生错误，大神信息：{}", master.toString(), e);
            throw e;
        }
    }

    /**
     * 删除大神
     *
     * @param id 大神ID
     */
    @Override
    @Transactional
    public void deleteMaster(Long id) {
        try {
            log.info("删除大神，大神ID：{}", id);
            masterMapper.deleteById(id);
        } catch (Exception e) {
            log.error("删除大神时发生错误，大神ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 创建大神
     *
     * @param master 大神信息
     */
    @Override
    @Transactional
    public void createMaster(Master master) {
        try {
            log.info("创建大神：{}", master.toString());
            masterMapper.insert(master);
        } catch (Exception e) {
            log.error("创建大神时发生错误，大神信息：{}", master.toString(), e);
            throw e;
        }
    }
}